# -*- coding: utf-8 -*-
import sys
sys.path.append('../..')
import json
from common.helper import Helper
import requests
from itertools import product
from db.mysql import MysqlClient
from common.config import *



class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "职场综合医疗保险"

    def genProductInfo(self):
        plan = {
            # 年龄段
            "year": ["18-40","41-50","51-65"],
            # 有无社保
            "socialSec": ["Y", "N"],
            # 保障期限
            "expire": ["一年"],
            # 交费类型
            "payType": ["年交"],
            # 意外身故、残疾
            "accidentDeath": ["100000","200000"],
            # 意外医疗
            "accidentMedical": ["0","10000","50000"],
            # 猝死
            "suddenDeath": ["100000","200000","500000"],
            # 疾病住院医疗
            "hospitalMedical": ["0","10000","40000"],
            # 重大疾病医疗
            "seriousMedical": ["0","50000","500000"],
            # 住院误工津贴
            "delayAllowance": ["20/天","50/天"],
            # 飞机意外
            "accidentPlane": ["0","1000000","5000000"],
            # 火车意外
            "accidentTrain": ["0","100000","1000000"],
            # 轮船意外
            "accidentShip": ["0","100000","1000000"],
            # 汽车意外
            "accidentPassenger": ["0","50000","2000000"]

        }
        number = 0 
        
        for v in product(plan["year"], plan["socialSec"], plan["expire"], plan["payType"], plan["accidentDeath"], plan["accidentMedical"], plan["suddenDeath"], plan["hospitalMedical"], plan["seriousMedical"], plan["delayAllowance"], plan["accidentPlane"], plan["accidentTrain"], plan["accidentShip"], plan["accidentPassenger"]):
            data = Helper.formatProductInfo(plan, v)
            # print( data )
            number+=1
        print( number )
                # self.mysql.cursor.execute(
                #     domain[self.name]["insert_sql"].format(**data))
                # self.mysql._conn.commit()
                # print(domain[self.name]["insert_sql"].format(**data))
    # 执行爬虫
    def requestJson(self, dt=None, mysql=None):
        data = {"insurantNum": [{"personNum": 1, "subjectType": 90}], "effectDate": {"birthDay": "1995-05-27", "addDays": "", "endDate": "2021-05-26", "beginDate": "2020-05-27"}, "planPackage": {
            "marketProductCode": "MP03000405", "packageCode": "PK00000528", "packageType": "01"}, "applyPeriod": {"term": 12, "termUnit": "M"}, "planDuty": [], "security": "1", "planSex": "01", "flowId": "L0N8sKAgeXgiOS8h"}
        payload = json.dumps(data)
        r = requests.post(
            url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
        print(r.text)
        if r.status_code == 200:
            print(r.text)


if __name__ == "__main__":
    s = Spider()
    s.genProductInfo()
